<template>
  <div>
    <el-page-header @back="back()" content="单位管理页面" style="margin-bottom: 10px;"/>
    <el-button type="primary" @click="add">新增</el-button>
    <el-button type="primary" @click="del">删除</el-button>
    <el-table
      :data="units.slice((currentPage-1)*pagesize,currentPage*pagesize)"
      style="width: 60vw;left: 10vw"
      @selection-change="handleSelectionChange">
      <el-table-column
        label="种类 ID"
        prop="id">
      </el-table-column>
      <el-table-column
        label="种类名称"
        prop="unit">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 40]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="units.length">
    </el-pagination>
    <add-unit ref="addUnits"></add-unit>
  </div>
</template>

<script>
    import AddUnit from "../goods_manager/AddUnit";
    export default {
        name: "UnitsManage",
      components: {AddUnit},
      data() {
        return {
          currentPage:1, //初始页
          pagesize:10,    //    每页的数据
          units:[],
        }
      },
      mounted:function () {
        this.$parent.show=false
        this.loadKinds()
      },
      methods:{
        // 初始页currentPage、初始每页数据数pagesize和数据data
        handleSizeChange: function (size) {
          this.pagesize = size;
        },
        handleCurrentChange: function(currentPage){
          this.currentPage = currentPage;
        },
        back(){
          this.$router.back();
        },
        add(){
          this.$refs.addUnits.dialogFormVisible=true
          this.loadKinds()
        },
        loadKinds(){
          var _this=this
          this.$axios.get('/units').then(resp => {
            if (resp && resp.status === 200) {
              _this.units=[]
              for(var i=0;i<resp.data.length;i++){
                if(resp.data[i].unit==null)
                  continue
                _this.units.push(resp.data[i])
              }
              //_this.units = resp.data
              //alert(JSON.stringify(_this.units))
            }
          })
        },
        del(){
          var _this=this
          this.$axios.post('/units/delete', this.selected_units()).then(resp => {
            if (resp && resp.status === 200) {
              this.loadKinds()
              this.$message({
                message:'删除成功',
                type:'success'
              })
            }
          }).catch(res=>{
            this.$message({
              message:'删除失败',
              type:'success'})
          })
        },
        handleSelectionChange(val) {
          this.multipleSelection = val;
          //alert(JSON.stringify(this.multipleSelection))
        },
        selected_units(){
          /*
        var units= {}
        for(var i=0;i<this.multipleSelection.length;i++){
          units['good'+i.toString()]=this.multipleSelection[i]
        }*/
          var units= []
          for(var i=0;i<this.multipleSelection.length;i++){
            units.push(this.multipleSelection[i].id)
          }
          return units
        }
      },
    }
</script>

<style scoped>

</style>
